function Plane() {
    var _this = this;

    //成员变量


    //需调用set函数初始化
    _this.centrePoint
        = null;
    _this.planeCanvas
        = null;
    _this.playerBelongs
        = null;
    _this.bulletCanvas
        = null;


    //可选赋值
    _this.secondaryWeaponPack
        = null;

    //默认赋值
    _this.modelUsed
        = new Model();
    _this.maxHP
        = 3000;
    _this.HP
        = 3000;
    _this.mainWeaponPack
        = new WeaponPack();
    _this.runningTrack
        = new RunningTrack();
    _this.deathEffects
        = new ArcEffects();
    _this.isDeath
        = false;
    _this.isDestroied
        = false;
    _this.needToDraw
        = true;
    _this.recoveryRate
        = 20;
    _this.collisionDamage
        = 300;
    _this.mark
        = 200;


    _this.init = function () {

        _this.deathEffects.fillColor.setRGBA(255, 100, 0, 0.8);
        _this.deathEffects.strokeColor.setRGBA(255, 100, 0, 0.8);

        _this.deathEffects.max_r = this.modelUsed.radius * 2;
        _this.deathEffects.risingIndex = 1.2;
    };

    this.init();
}

Plane.prototype = {

    constructor: Plane,

    setPlayerBelongs: function (player) {
        this.playerBelongs = player;
        if (this.mainWeaponPack != null)this.mainWeaponPack.playerBelongs = player;
        if (this.secondaryWeaponPack != null)this.secondaryWeaponPack.playerBelongs = player;
    },

    setCentrePoint: function (point) {
        this.centrePoint = point;
        this.runningTrack.centrePoint = point;
        this.deathEffects.point = this.centrePoint;
        if (this.mainWeaponPack != null)this.mainWeaponPack.centrePoint = point;
        if (this.secondaryWeaponPack != null)this.secondaryWeaponPack.centrePoint = point;
    },

    setPlaneCanvas: function (canvas) {
        this.planeCanvas = canvas;
        canvas.objs.push(this);
        canvas.allObj.objs.push(this);
    },

    setBulletCanvas: function (canvas) {
        this.bulletCanvas = canvas;
        if (this.mainWeaponPack != null)this.mainWeaponPack.canvas = canvas;
        if (this.secondaryWeaponPack != null)this.secondaryWeaponPack.canvas = canvas;
    },


    changeSecondaryWeaponPack: function (GivingPack) {
        this.endOfShoot();
        var canvas = this.bulletCanvas;
        if (GivingPack != null) {
            this.secondaryWeaponPack = new GivingPack();
            this.setCentrePoint(this.centrePoint);
            this.setBulletCanvas(canvas);
            this.setPlayerBelongs(this.playerBelongs);
            this.startShoot();
        } else {
            this.mainWeaponPack = null;
        }

    },

    changeMainWeaponPack: function (GivingPack) {
        this.endOfShoot();
        var canvas = this.bulletCanvas;
        if (GivingPack != null) {
            this.mainWeaponPack = new GivingPack();
            this.setCentrePoint(this.centrePoint);
            this.setBulletCanvas(canvas);
            this.setPlayerBelongs(this.playerBelongs);
            this.startShoot();
        } else {
            this.mainWeaponPack = null;
        }
    },

    checkEndShoot: function () {
        if (this.mainWeaponPack == null || this.mainWeaponPack.clip <= 0) {
            this.changeMainWeaponPack(WeaponPack);
        }
    },

    beInjured: function (damage) {
        this.HP -= damage;
        player.writeInfo();
        if (this.HP <= 0) {
            this.isDeath = true;
            this.endOfShoot();
            if (this.playerBelongs.id == 0) {
                this.playerBelongs.overGame();
            } else if (this.playerBelongs.id == 1) {
                if (Math.random() * 100 < (enemy.difficulty * 0.25) + 5) {
                    var randomAllPacks = [GivingPack, RandomGivingPack, RandomSGivingPack, RayGivingPack, FollowGivingPack, DefenseGivingPack];
                    var givingPack = new randomAllPacks[parseInt(Math.random() * (randomAllPacks.length - 0.01))]();
                    givingPack.setCentrePoint(this.centrePoint);
                    givingPack.setPlaneCanvas(this.planeCanvas);
                }
            }
            if (this.playerBelongs != 0) {
                player.mark += this.mark;
            }
        }
        if (this.HP > this.maxHP) this.HP = this.maxHP;
    },

    isInnerWindows: function () {

        var width = gameBox.offsetWidth;
        var height = gameBox.offsetHeight;

        var destroyPoint = new Point(width * 0.1, height * 0.1);

        var isInnerWindows = true;

        isInnerWindows = isInnerWindows && this.centrePoint.x < width + destroyPoint.x;
        isInnerWindows = isInnerWindows && this.centrePoint.x > 0 - destroyPoint.x;
        isInnerWindows = isInnerWindows && this.centrePoint.y < height + destroyPoint.y;
        // return true;
        return isInnerWindows && this.centrePoint.y > 0 - destroyPoint.y;
    },

    trackChanges: function () {
        //改变轨迹事件
    },

    runWithTrack: function () {

        if (!this.isDeath) {
            this.trackChanges();
            this.setCentrePoint(this.runningTrack.trackStep());
            if (this.modelUsed.isFaceDeg)
                this.modelUsed.faceDeg = this.runningTrack.direction;
            if (!this.isInnerWindows() && this.playerBelongs.id != 0) {
                this.needToDraw = false;
                this.isDestroied = true;
            }
        }
        if (this.isDestroied)this.isDeath = true;
        if (this.isDestroied) {
            this.endOfShoot();
            this.needToDraw = false;
            delete this;
        }


    },

    checkInnerWindows: function (w, h) {

        var width = gameBox.offsetWidth;
        var height = gameBox.offsetHeight;
        //检查是否在屏幕中
        if (this.centrePoint.x > width - w * .5)this.centrePoint.x = width - w * .5;
        if (this.centrePoint.x < w * 0.5)this.centrePoint.x = w * 0.5;
        if (this.centrePoint.y > height - h * 0.5)this.centrePoint.y = height - h * 0.5;
        if (this.centrePoint.y < h * 0.5)this.centrePoint.y = h * 0.5;
    },

    draw: function () {
        var _this = this;
        this.runWithTrack();
        var ctx = this.planeCanvas.ctx;

        if (!this.isDeath) {
            ctx.beginPath();
            this.modelUsed.preImg(function () {

                var winWidth = window.innerWidth;
                var model = _this.modelUsed;
                var img = model.img;
                var realWidth = winWidth * model.size;
                var realHeight = realWidth * (img.height / img.width);
                if (_this.playerBelongs.id == 0)
                    _this.checkInnerWindows(realWidth, realHeight);
                var correctPoint = new Point(_this.centrePoint.x - realWidth * 0.5, _this.centrePoint.y - realHeight * 0.5);

                ctx.save();
                ctx.translate(_this.centrePoint.x, _this.centrePoint.y);
                ctx.rotate(_this.modelUsed.faceDeg * Math.PI / 180);//旋转
                ctx.translate(-_this.centrePoint.x, -_this.centrePoint.y);
                ctx.drawImage(img, correctPoint.x, correctPoint.y, realWidth, realHeight);
                ctx.restore();

            });
        } else {
            //死亡特效
            this.deathEffects.draw(ctx);
            if (this.deathEffects.isEnd) {
                this.isDestroied = true;
            }
        }


    },
    whileShootEnd: function () {
        if (this.mainWeaponPack != null && this.mainWeaponPack.isUsedOff) {
            this.changeMainWeaponPack(WeaponPack);
        }
        if (this.secondaryWeaponPack != null && this.secondaryWeaponPack.isUsedOff) {
            this.changeSecondaryWeaponPack(null);
        }
    },
    startShoot: function () {
        var _this = this;
        if (this.mainWeaponPack != null)this.mainWeaponPack.startShoot(function () {
            _this.whileShootEnd();
        });
        if (this.secondaryWeaponPack != null)this.secondaryWeaponPack.startShoot(function () {
            _this.whileShootEnd();
        });
    },
    endOfShoot: function () {
        if (this.mainWeaponPack != null)this.mainWeaponPack.endOfShoot();
        if (this.secondaryWeaponPack != null)this.secondaryWeaponPack.endOfShoot();
    }
};


function EnemyPlane() {
    var _this = this;
    Plane.call(this);

    _this.modelUsed
        = new EnemyModel();
    _this.maxHP
        = 300;
    _this.HP
        = 300;
    _this.mainWeaponPack
        = new EnemyNormalPack();
    _this.runningTrack
        = new EnemyRunningTrack();
    this.secondaryWeaponPack = null;
    _this.collisionDamage
        = 300;
    _this.mark
        = 200;
    _this.init = function () {

        _this.deathEffects.fillColor.setRGBA(100, 100, 255, 0.8);
        _this.deathEffects.strokeColor.setRGBA(100, 100, 255, 0.8);

        _this.deathEffects.max_r = _this.modelUsed.radius * 1;
        _this.deathEffects.risingIndex = 1.1;
    };

    this.init();
}
Extend(Plane, EnemyPlane);



